﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public partial class Cost
    {
        public static Cost FindByCostID_IncludeDeleted(linqDatabaseDataContext db, int CostID)
        {
            var subquery = (from u in db.Costs where u.ID == (from v in db.Costs where v.CostID == u.CostID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.CostID == CostID select u).SingleOrDefault());
        }

        public static Cost FindByCostID(linqDatabaseDataContext db, int CostID)
        {
            var subquery = (from u in db.Costs where u.ID == (from v in db.Costs where v.CostID == u.CostID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.CostID == CostID select u).SingleOrDefault());
        }

        public static List<Cost> ToList_IncludeDeleted(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Costs where u.ID == (from v in db.Costs where v.CostID == u.CostID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery select u).ToList());
        }

        public static List<Cost> ToList(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Costs where u.ID == (from v in db.Costs where v.CostID == u.CostID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted select u).ToList());
        }

        public static List<Cost> ToListByRequestID_IncludeDeleted(linqDatabaseDataContext db, int RequestID)
        {
            var subquery = (from u in db.Costs where u.ID == (from v in db.Costs where v.CostID == u.CostID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where (from v in db.sCosts where v.RequestID == RequestID select v.ID).Contains(u.CostID) select u).ToList());
        }

        public static List<Cost> ToListByRequestID(linqDatabaseDataContext db, int RequestID)
        {
            var subquery = (from u in db.Costs where u.ID == (from v in db.Costs where v.CostID == u.CostID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where (from v in db.sCosts where v.RequestID == RequestID select v.ID).Contains(u.CostID) && !u.IsDeleted select u).ToList());
        }

        public static List<Cost> HistoryToListByCostID(linqDatabaseDataContext db, int CostID)
        {
            return (from u in db.Costs where u.CostID == CostID orderby u.Timestamp ascending select u).ToList();
        }
    }
}
